package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Attendance;
import cn.wolfcode.crm.query.AttendanceQueryObject;
import cn.wolfcode.crm.service.IAttendanceService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/attendance")
public class AttendanceController {

    private Long id;
    private Long beginTime;
    @Autowired
    IAttendanceService attendanceService;

    @RequestMapping("/view")
    public String view() {

        return "/employee_management/attendance";
    }

    @RequestMapping("/query")
    @ResponseBody
    public Object query(AttendanceQueryObject qo) {

        return attendanceService.query(qo);
    }

    @RequestMapping("/save")
    @ResponseBody
    public JsonResult save() {
        Attendance attendance = new Attendance();
        Date date = new Date();
        beginTime = date.getTime();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        String timeIn = format.format(date);
        attendance.setDate(date);
        attendance.setEmployeename(UserContext.getCurrentUser().getUsername());
        attendance.setDeptname(UserContext.getCurrentUser().getDept().getName());
        attendance.setTimein(timeIn);
        attendance.setTimeout(null);
        attendance.setState(null);
        try {
            attendanceService.insert(attendance);
            id = attendanceService.selectIdByName(UserContext.getCurrentUser().getUsername());
//            System.out.println("查询出来的id核对:" + id);
        } catch (Exception ex) {
            ex.printStackTrace();
            return new JsonResult().mark("操作失败");
        }
        return new JsonResult();
    }

    @RequestMapping("/updateTimeOut")
    @ResponseBody
    public JsonResult update() {
        //首先获取到当前登录的用户
        Date date2 = new Date();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        String timeOut = format.format(date2);
        System.out.println("下班时间" + timeOut);
        //现在可以来计算时间了----->设置state是true/false还是null 工作时间:61200000
        Boolean state = null;

        long standard = 61200000;
        //上班时间毫秒数
        Attendance attendance = attendanceService.selectByPrimaryKey(id);

        //下班时间毫秒数
        long endTime = date2.getTime();

        long workTime = endTime - beginTime;
        System.out.println("工作时间:" + workTime);
        if (workTime >= standard) {
            state = true;
        } else if (workTime < standard) {
            state = false;
        }

        try {
            attendanceService.updateTime(timeOut, state, id);
        } catch (Exception ex) {
            ex.printStackTrace();
            return new JsonResult().mark("操作失败");
        }
        return new JsonResult();
    }


    @RequestMapping("/destroy")
    @ResponseBody
    public Object destroy() {
        try {
            attendanceService.destroy();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败");
        }
        return new JsonResult();
    }


}
